package com.lap.web.starter.ip;

import jakarta.servlet.http.HttpServletRequest;
import lombok.experimental.UtilityClass;

@UtilityClass
public class IpUtils {

  private static final String[] IP_HEADERS = {
    "X-Forwarded-For",
    "X-Real-IP",
    "HTTP_CLIENT_IP",
    "HTTP_X_FORWARDED_FOR",
    "Proxy-Client-IP",
    "WL-Proxy-Client-IP"
  };

  public static String getClientIp(HttpServletRequest request) {
    for (String header : IP_HEADERS) {
      String ip = request.getHeader(header);
      if (ip != null && !ip.isBlank() && !"unknown".equalsIgnoreCase(ip)) {
        return ip.split(",")[0].trim();
      }
    }
    return request.getRemoteAddr();
  }
}
